/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.ThoGiaCongDTO;

/**
 *
 * @author tuan
 */
public class ThoGiaCongDAO {
    private static final String MaThoGC = "MaTGC000";
    private static final int lengthMa = 8;
    
    public ThoGiaCongDAO() {
    }
    
    public String getNewMaThoGC() throws SQLException {
        String MaTGC = "";
        int maxMa = 0;
        ResultSet rs = SQLServerConnect.getInstance().getDataFromStore("GetAllThoGCFromDatabase", null);
        while (rs.next()) {
            int temp = Integer.parseInt( rs.getString("MATHO").substring(lengthMa) );
            if (maxMa < temp) {
                maxMa = temp;
            }
        }
        maxMa ++;
        MaTGC = MaTGC.concat(MaThoGC + maxMa);
        return MaTGC;
    }
    
    //if get ThoGiaCong by MaTho then thogc.Ten = null and diachi = null
    public ArrayList<ThoGiaCongDTO> GetThoGiaCongByRule(ThoGiaCongDTO thogc) throws SQLException {
        ArrayList<ThoGiaCongDTO> listTho = new ArrayList<>();
        Object[] objs = { thogc.getMaTho(), thogc.getTenTho(), thogc.getDiaChi() };
        ResultSet result;
        result = SQLServerConnect.getInstance().getDataFromStore("GetThoGiaCongByRuleFromDatabase(?, ?, ?)", objs);
        ThoGiaCongDTO ThoGiaCong;
        while (result.next()) {
            ThoGiaCong = new ThoGiaCongDTO(result.getString("MATHO"), 
                        result.getString("TENTHO"), result.getString("DIACHI"));
            listTho.add(ThoGiaCong);
        }
        return listTho;
    }
    
    public ArrayList<ThoGiaCongDTO> GetAllThoGiaCong() throws SQLException {
        ArrayList<ThoGiaCongDTO> listTho = new ArrayList<>();
        ResultSet result;
        result = SQLServerConnect.getInstance().getDataFromStore("GetAllThoGiaCongFromDatabase", null);
        ThoGiaCongDTO ThoGiaCong;
        while (result.next()) {
            ThoGiaCong = new ThoGiaCongDTO(result.getString("MATHO"), 
                        result.getString("TENTHO"), result.getString("DIACHI"));
            listTho.add(ThoGiaCong);
        }
        return listTho;
    }
    
    public ThoGiaCongDTO getThoGiaCongByMaTho(String MaTho) throws SQLException {
        Object[] objs = { MaTho };
        ResultSet result;
        result = SQLServerConnect.getInstance().getDataFromStore("GetThoGiaCongByMaThoFromDatabase(?)", objs);
        ThoGiaCongDTO ThoGiaCong = null;
        while (result.next()) {
            ThoGiaCong = new ThoGiaCongDTO(result.getString("MATHO"), 
                        result.getString("TENTHO"), result.getString("DIACHI"));
        }
        return ThoGiaCong;
    }
    
    public int UpdateThoGiaCong( ThoGiaCongDTO thogc ) {
        Object[] objs = { thogc.getMaTho(), thogc.getTenTho(), thogc.getDiaChi() };
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateThoGiaCongFromDatabase(?, ?, ?)", objs);
    }
    
    public int DeleteThoGiaCong( ThoGiaCongDTO thogc ) {
        Object[] objs = { thogc.getMaTho() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteThoGiaCongByMaThoDVToDatabase(?)", objs);
    }
    
    public int InsertThoGiaCong( ThoGiaCongDTO thogc ) {
        Object[] objs = { thogc.getMaTho(), thogc.getTenTho(), thogc.getDiaChi() };
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertThoGiaCongFromDatabase(?, ?, ?)", objs);
    }
}
